package com.ly.algorithm.DataStructures.tree.binarysorttree;

/**
 * @Classname BinarySortTree
 * @Description TODO
 * @Date 2020/5/3 10:32
 * @Author 冷心影翼
 */
public class BinarySortTree {

    public Node root;

    public void add(Node node) {
        if(root!=null) {
            root.add(node);
        }else {
            root = node;
        }
    }

    public void infixOrder() {
        if (root!=null) {
            root.infixOrder();
        }
    }

    public Node search(int data) {
        if (root!=null) {
            return root.search(data);
        }
        return null;
    }

    public Node searchParent(int data) {
        if (root!=null) {
            return root.searchParent(data);
        }
        return null;
    }

    public void delete(int data) {
        if(root!=null) {
            if(root.left == null && root.right == null) {
                Node search = search(data);
                if(search!=null) {
                    root = null;
                }
                return;
            }
            if(root.left == null && root.right!=null) {
                Node search = search(data);
                if(search!=null) {
                    root = root.right;
                }
                return;
            }
            if(root.right == null && root.left!=null) {
                Node search = search(data);
                if(search!=null) {
                    root = root.left;
                }
                return;
            }
            if (root.right != null && root.left!=null) {
                root.delete(data);
            }
        }


    }
}

class Node{
    int data;
    Node left;
    Node right;

    public Node(int data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                '}';
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }
        if(node.data<data) {
            if(left == null) {
                left = node;
            }else {
                left.add(node);
            }
        }else {
            if(right == null) {
                right = node;
            }else {
                right.add(node);
            }
        }
    }

    public void infixOrder() {
        if(left!=null) {
            left.infixOrder();
        }
        System.out.println(this);
        if(right != null) {
            right.infixOrder();
        }
    }

    public Node search(int data) {
        Node res = null;
        if(data<this.data && this.left != null) {
            res =  left.search(data);
        }
        if (data == this.data) {
            res = this;
        }
        if(data>this.data && this.right!=null){
            res = right.search(data);
        }
        return res;
    }

    public Node searchParent(int data) {
        Node res = null;
        if(data<this.data && this.left != null) {
            if(this.left.data == data) {
                res = this;
            }else {
                res = left.searchParent(data);
            }
        }
        if(data>this.data && this.right!=null){
            if(this.right.data == data) {
                res =  this;
            }else {
                res = right.searchParent(data);
            }
        }
        return res;
    }



    public void delete(int data) {
        Node cur = search(data);
        Node parent = searchParent(data);
        if(cur == null) {
            return;
        }
        if(cur.left == null && cur.right == null) {
            if(parent.left!=null && parent.left.data == cur.data) {
                parent.left = null;
                return;
            }else {
                parent.right = null;
                return;
            }
        }
        if(cur.left == null && cur.right!=null) {
            if(parent.left!=null && parent.left.data == cur.data) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }
        if(cur.right == null && cur.left!=null) {
            if(parent.left!=null && parent.left.data == cur.data) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }
        if(cur.right != null && cur.left!=null) {
            Node node = cur;
            cur = cur.right;
            while (cur.left != null ) {
                cur = cur.left;
            }
            delete(cur.data);
            node.data = cur.data;
        }
    }
}

class Test {
    public static void main(String[] args) {
        BinarySortTree binarySortTree = new BinarySortTree();
        int[] arr = {7,3,1,42,12,41,5,6,9};
        for(int a:arr) {
            binarySortTree.add(new Node(a));
        }
//        binarySortTree.infixOrder();
//        System.out.println(binarySortTree.search(12));
//        System.out.println(binarySortTree.searchParent(7));
        binarySortTree.delete(7);
        binarySortTree.infixOrder();
    }
}
